package clue

import (
	"context"
	"errors"
	"git.mycaigou.com/gfyx/common/authorize"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/common/constants"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/dto/clue"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/repository/bigdata"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/utils"
	"git.myscrm.cn/golang/stark/v4"
	"strconv"
)

/**
 * @Author: xiuliu
 * @Description:
 * @File:  customer_monitor_service
 * @Version: 1.0.0
 * @Date: 2023/12/14 11:18
 */

type CustomerMonitorService struct {
	bigdataRepository bigdata.BigdataRepositoryIface
}

func NewCustomerMonitorService(bigdataRepository bigdata.BigdataRepositoryIface) CustomerMonitorServiceIface {
	return &CustomerMonitorService{
		bigdataRepository: bigdataRepository,
	}
}

func (c *CustomerMonitorService) GetCustomerMonitorList(ctx context.Context, request *clue.GetCustomerMonitorRequest) (*clue.GetCustomerMonitorResponse, error) {
	tenantCode := ctx.Value(authorize.TENANT_CODE_KEY).(string)
	userId := ctx.Value(constants.LoginUserId).(int64)
	request.TenantCode = tenantCode
	request.TenantUserId = strconv.FormatInt(userId, 10)

	if request.Page <= 0 {
		request.Page = 1
	}
	if request.PageSize <= 0 {
		request.PageSize = 20
	}

	if request.CustomerName != "" {
		request.CustomerName = "%" + request.CustomerName + "%"
	}

	if request.StartDate != "" {
		startTime, err := utils.StrToTime(request.StartDate)
		if err != nil || startTime.IsZero() {
			return nil, errors.New("动态开始时间格式错误")
		}
		request.StartDate = utils.TimeFormat(startTime)
	}

	if request.EndDate != "" {
		endTime, err := utils.StrToTime(request.EndDate)
		if err != nil || endTime.IsZero() {
			return nil, errors.New("动态结束时间格式错误")
		}
		request.EndDate = utils.TimeFormat(endTime)
	}

	result, err := c.bigdataRepository.GetCustomerMonitorList(ctx, &bigdata.GetCustomerMonitorRequest{
		TenantCode:   request.TenantCode,
		TenantUserId: request.TenantUserId,
		Page:         request.Page,
		PageSize:     request.PageSize,
		CustomerName: request.CustomerName,
		FeedTypeCode: request.FeedTypeCode,
		AreaCode:     request.AreaCode,
		StartDate:    request.StartDate,
		EndDate:      request.EndDate,
	})
	if err != nil {
		stark.Logger.Errorf(ctx, "GetCustomerMonitorList errors:%+v", err)
		return nil, err
	}
	resp := &clue.GetCustomerMonitorResponse{
		List:  []*clue.CustomerMonitorItem{},
		Total: 0,
	}
	if result != nil {
		resp.Total = result.Total
		for _, item := range result.List {
			resp.List = append(resp.List, &clue.CustomerMonitorItem{
				City:          item.City,
				CityCode:      item.CityCode,
				CreditCode:    item.CreditCode,
				CustomerName:  item.CustomerName,
				FeedDate:      item.FeedDate,
				FeedType:      item.FeedType,
				FeedTypeCode:  item.FeedTypeCode,
				ID:            item.ID,
				JumpID:        item.JumpID,
				Province:      item.Province,
				ProvinceCode:  item.ProvinceCode,
				Title:         item.Title,
				TenantCode:    item.TenantCode,
				TenantUserUID: item.TenantUserUID,
			})
		}
	}

	return resp, nil
}
